package mgodb

import (
	"jgt-oper/src/model"
	"jgt-oper/src/schema"
	"jgt-oper/src/service/mongo"

	"github.com/facebookgo/inject"
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"github.com/pkg/errors"
	"time"
)

//	人员授权区域表
const cUserPark = "oper_user_park"

// UserPark 用户授权区域管理
type UserPark struct {
	DB *mongo.DB
}

// Init 初始化
func (a *UserPark) Init(g *inject.Graph, db *mongo.DB) {
	a.DB = db

	// 初始化mongo索引
	db.C(cUser, func(c *mgo.Collection) error {
		c.EnsureIndexKey("record_id", "deleted")
		c.EnsureIndex(mgo.Index{
			Key:    []string{"record_id"},
			Unique: true,
		})
		return nil
	})

	var val model.IUserPark = a
	g.Provide(&inject.Object{Value: val, Name: "IUserPark"})
}

// QueryPage 查询分页
func (a *UserPark) QueryPage(param *schema.UserEntranceParams, current, pageSize uint) (int, []*schema.ResponseItemUserEntrance, error) {

	queryString := []bson.M{
		{"$match": bson.M{"deleted": 0}},
		{"$lookup": bson.M{"from": "oper_user", "localField": "user_id", "foreignField": "record_id", "as": "oper_user"}},
		{"$lookup": bson.M{"from": "auth_user", "localField": "user_id", "foreignField": "_id", "as": "auth_user"}},
		{"$unwind": bson.M{"path": "$oper_user", "preserveNullAndEmptyArrays": true}},
		{"$unwind": bson.M{"path": "$auth_user", "preserveNullAndEmptyArrays": true}},
		{"$match": bson.M{"oper_user.deleted": 0, "auth_user.deleted": 0}},
		{"$lookup": bson.M{"from": "oper_enterprise", "localField": "oper_user.enterprise_id", "foreignField": "record_id", "as": "oper_enterprise"}},
		{"$unwind": bson.M{"path": "$oper_enterprise", "preserveNullAndEmptyArrays": true}},
		{"$match": bson.M{"oper_enterprise.deleted": 0}},
		{"$addFields": bson.M{"enterprise_name": "$oper_enterprise.name", "nick_name": "$oper_user.nickname",
			"real_name": "$oper_user.real_name", "gender": "$oper_user.gender",
			"contract_tel": "$auth_user.tel", "user_type": "$oper_user.user_type",
			"operator": "", "operator_time": 0}},
		{"$project": bson.M{"record_id": 1, "created": 1, "deleted": 1, "user_id": 1,
			"enterprise_park": 1, "extend_park": 1, "reduce_park": 1, "enterprise_name": 1,
			"nick_name": 1, "real_name": 1, "gender": 1, "contract_tel": 1, "user_type": 1,
			"operator": 1, "operator_time": 1}},
	}
	var items []*schema.ResponseItemUserEntrance
	err := a.DB.C(cUserPark, func(c *mgo.Collection) error {
		return c.Pipe(queryString).All(&items)
	})
	if err != nil {
		return 0, nil, errors.Wrapf(err, "查询数据发生错误")
	}

	return 0, items, nil
}

// CreateParkEntrance 创建特殊人员授权区域
func (a *UserPark) CreateParkEntrance(userParkEntrance *schema.SpecialUserPark) error {
	err := a.DB.C(cUserPark, func(c *mgo.Collection) error {
		id, err := a.DB.IncrID(cUserPark)
		if err != nil {
			return err
		}
		userParkEntrance.ID = id
		return c.Insert(userParkEntrance)
	})
	if err != nil {
		return errors.Wrapf(err, "创建数据发生错误")
	}

	return nil
}

// UpdateParkEntrance 更新特殊人员的授权区域和门禁
func (a *UserPark) UpdateParkEntrance(userParkEntrance *schema.ParamUserParkEntrance) error {

	err := a.DB.C(cUserPark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"user_id": userParkEntrance.UserID, "deleted": 0},
			bson.M{"$set": bson.M{"parks": userParkEntrance.Parks, "entrances_frozen": userParkEntrance.EntrancesFrozen}})
	})
	if err != nil {
		return err
	}
	return nil
}

// GetUserPark 查询特殊人员的授权区域
func (a *UserPark) GetUserPark(userID string) (*schema.SpecialUserPark, error) {
	var (
		item *schema.SpecialUserPark
		err  error
	)
	item = new(schema.SpecialUserPark)
	err = a.DB.C(cUserPark, func(c *mgo.Collection) error {
		return c.Find(bson.M{"user_id": userID, "deleted": 0}).One(item)
	})
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return item, nil
}

// RemoveUserPark 删除用户的授权区域对应关系
func (a *UserPark) RemoveUserPark(userID string) error {
	err := a.DB.C(cUserPark, func(c *mgo.Collection) error {
		return c.Update(bson.M{"user_id": userID, "deleted": 0}, bson.M{"$set": bson.M{"deleted": time.Now().Unix()}})
	})
	if err != nil {
		return err
	}
	return nil
}
